<template>
  <div class="experience-level">
    <page-header title="经验等级">
      <page-search @search="getData">
        <el-button @click="showDialog()">添加经验等级</el-button>
      </page-search>
    </page-header>
    <el-table :data="tableData" :max-height="clientHeight - 250">
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :min-width="column.minWidth"
        :label="column.label">
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="120px"
      >
        <template slot-scope="{ row }">
          <action-text @click="showDialog(row)">修改</action-text>
          <action-text @click="deleteData(row)" type="danger">删除</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <formDialog
      ref="dialog"
      :initFormData="formData"
      :isCreate="isCreate"
      @confirm="getData"
    />
  </div>
</template>
<script>
import { getExperienceLevelList, deleteExperienceLevel } from '@/api/base-config';
import paginationMixin from '@/mixins/pagination';
import formDialog from './dialog';

const initFormData = {
  name: '',
  minVal: undefined,
  maxVal: undefined,
};

export default {
  data() {
    return {
      columns: [
        { prop: 'name', label: '等级名称', minWidth: '100px' },
        { prop: 'minVal', label: '最低经验值', minWidth: '100px' },
        { prop: 'maxVal', label: '最高经验值', minWidth: '100px' },
        { prop: 'createTime', label: '创建时间', minWidth: '140px' },
      ],
      tableData: [],
      isCreate: false,
      formData: {
        ...initFormData,
      },
    };
  },
  components: {
    formDialog,
  },
  mixins: [paginationMixin],
  methods: {
    showDialog(data) {
      if (data) {
        this.formData = { ...data };
        this.isCreate = false;
      } else {
        this.formData = { ...initFormData };
        this.isCreate = true;
      }
      this.$refs.dialog.dialogVisible = true;
    },
    async deleteData(row) {
      this.$confirm('确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleteExperienceLevel({
          experienceLevelId: row.id,
        });
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    async getData() {
      const result = await getExperienceLevelList(this.searchParams);
      const { total, list } = result;
      this.total = total;
      this.tableData = list && list.map((item) => {
        const { createTime } = item;
        return {
          ...item,
          createTime: this.$formatTime(createTime),
        };
      });
    },
  },
  async created() {
    await this.getData();
  },
};
</script>
